﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;

namespace COER
{
    public partial class UC_SystemSetting : UserControl
    {
        public UC_SystemSetting()
        {
            InitializeComponent();

            GetControls(this.Controls);
        }

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                //if (item is DLLPC.PCAP.PCButton && item.Tag?.ToString().Split('|')[0] == "btn")
                //{
                //    ((DLLPC.PCAP.PCButton)item).OperationRecord = Kernel.OPRec;
                //}

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        public void ShowForm()
        {
            ParamGrid.SelectedObject = ParameterHelper.SysParam;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (ProcedureHelper_EQ.SysGridRefresh)
            {
                ParamGrid.Refresh();
                ProcedureHelper_EQ.SysGridRefresh = false;
            }

            if (this.Visible == false) return;

            if (Kernel.OP.Level > 0)
            {
                btnPWExit.Visible = true;
                lblPWStatus.Text = "工程师模式";
            }
            else
            {
                btnPWExit.Visible = false;
                lblPWStatus.Text = "普通模式";
            }

            if (DateTime.Now.Subtract(dtStart).TotalMinutes >= 10)
            {
                Kernel.PW = 0;
                Kernel.OP.Level = 0;
            }

            btnSave.Enabled = Kernel.OP.Level > 0;
        }

        FormPassword frm;
        DateTime dtStart = DateTime.Now;
        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');

            switch (info[1])
            {
                case "PW":
                    {
                        if (frm == null)
                        {
                            frm = new FormPassword();
                        }

                        dtStart = DateTime.Now;
                        frm.ShowForm();
                    }
                    break;
                case "PWExit":
                    {
                        Kernel.PW = 0;
                        Kernel.OP.Level = 0;
                    }
                    break;
                case "Load":
                    {
                        ParameterHelper.SysParam = ParameterHelper.mSysDAL.Load();
                        ParamGrid.SelectedObject = ParameterHelper.SysParam;
                        MessageBox.Show("参数加载完成！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    break;
                case "Save":
                    {
                        ParameterHelper.SysParam = ParamGrid.SelectedObject as SystemModel;
                        ParameterHelper.mSysDAL.Save(ParameterHelper.SysParam);
                        MessageBox.Show("参数保存完成！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ParamGrid.Refresh();
                    }
                    break;
            }
        }
    }
}
